<?php
	$validation_rules = array(
		"onair_episode_no" => array(
			"required" => true,
            'positiveInt' => true,
            'epsExist' => true,
            'maxlength' => 9
		),
        'programme_title_cn' => array(
            'maxlength' => 255
        ),
        'programme_title_en' => array(
            'maxlength' => 255
        ),
        'programme_path' => array(
            'required' => true,
            #'path' => true,
            'maxlength' => 255
        ),
		"venue_name_chinese" => array(
			"maxlength" => '255'
		),
		"venue_name_english" => array(
			"maxlength" => '255'
		),
        'country_autocomplete' => array(
            'inAcList' => true
        ),
        'province_state_autocomplete' => array(
            'inAcList' => true
        ),
		"city" => array(
			"maxlength" => '255'
		),
		"street_line_1" => array(
			"maxlength" => '255'
		),
		"street_line_2" => array(
			"maxlength" => '255'
		),
		"phone_number" => array(
			"maxlength" => '255'
		),
		"fax_number" => array(
			"maxlength" => '255'
		),
		"url" => array(
			"maxlength" => '255'
		),
		"email" => array(
			"maxlength" => '255'
		),
        'publish_datetime[]' => array(
            'required' => TRUE,
        ),
        'expire_datetime[]' => array(
            'required' => TRUE,
        ),        
	);
?>

<?php

    if ($act==='view') {
        FormHelper::setReadonly(true);
        $validation_rules = array();
    }

	$address = isset($data['address']) ? $data['address'] : "";
	$episode = isset($data['episode']) ? $data['episode'] : "";
	$programme = isset($data['programme']) ? $data['programme'] : "";
	$address_json_list = isset($data['address_json_list']) ? $data['address_json_list'] : array();
	$form_action = ($act === 'view') ? "info_switch.html?type=address" : ( ($act === 'add') ? "add_address.html?act=add&type=address" : "update_address.html?type=address&act=edit");
	$publish = isset($data['unpublish']) ? $data['unpublish'] : 0;
	$from    = isset($data['from']) ? $data['from'] : '';
   
?>

<div id="address" class="mod ui-widget-content ui-helper-clearfix ui-corner-all <?php echo $act; ?>">

    <div class="hd ui-widget-header ui-helper-clearfix ui-corner-all"><h2><?php echo $act_text; ?> Address</h2></div>

    <div class="bd">
    <?php FormHelper::StartForm('ane', AC::PDES_BASE_URL.$form_action) ?>
	    <?php FormHelper::ErrorMessages($errors) ?>
		<?php if($act != 'add') FormHelper::HiddenField('id', $address->id) ?>
		<?php if($act != 'add') FormHelper::HiddenField('episode_id', $address->episode_id) ?>
		<?php FormHelper::HiddenField('check_used', $data['check_used']) ?>
		<?php FormHelper::HiddenField('status', $address->status) ?>
		<?php FormHelper::HiddenField('created_at', $address->created_at) ?>
		<?php FormHelper::HiddenField('created_by', $address->created_by) ?>
        <?php FormHelper::HiddenField('from', $from) ?>
		<?php FormHelper::HiddenField('modified_at', $address->modified_at) ?>
		<?php FormHelper::HiddenField('modified_by', $address->modified_by) ?>
    	<fieldset class="ui-widget-content">
            <legend class="ui-widget-header">Find Programme Record</legend>
<?php
	if ($act != 'add')
	{
?>
			<?php FormHelper::ProtectedField($programme->id, 'Programme ID');?>
            <?php FormHelper::ProtectedField($address->id, 'Address ID');?>
            <?php 
				FormHelper::ProtectedField($programme->zh_programme_name, 'Programme Title (Chinese)');
				FormHelper::ProtectedField($programme->en_programme_name, 'Programme Title (English)');
				FormHelper::ProtectedField($programme->path, 'Programme Path') ?>
			<?php FormHelper::ProtectedField($episode->onair_episode_no, 'On Air Episode No.') ?>
			<?php FormHelper::HiddenField('programme_id', $programme->id) ?>
			<?php FormHelper::HiddenField('onair_episode_no', $episode->onair_episode_no) ?>
<?php
	}
	else
	{
?>
            
            <input id="programme_id" name="programme_id" type="hidden" value="<?php echo $address->programme_id;?>" />
			<input id="programme_eps" type="hidden" value="<?php echo $address->onair_episode_no;?>" />

            <div class="form-item">
                <span class="grid-l"><label><ins>**</ins>Programme Title (Chinese)</label> :</span>
                <span class="grid-r">
                <input id="programme_title_cn" name="programme_title_cn" type="text" class="text" autocomplete="off" 
                    value="<?php echo htmlentities($address->programme_title_cn, ENT_QUOTES, 'UTF-8');?>" />
                </span>
            </div>
            <div class="form-item">
                <span class="grid-l"><label><ins>**</ins>Programme Title (English)</label> :</span>
                <span class="grid-r">
                <input id="programme_title_en" name="programme_title_en" type="text" class="text" autocomplete="off" 
                    value="<?php echo htmlentities($address->programme_title_en, ENT_QUOTES, 'UTF-8');?>" /> 
                </span>
            </div>

            <div class="form-item">
                <span class="grid-l"><label><ins>*</ins>Programme Path</label> :</span>
                <span class="grid-r">
                <input id="programme_path" name="programme_path" type="text" class="text" autocomplete="off" 
                    value="<?php echo htmlentities($address->programme_path, ENT_QUOTES, 'UTF-8');?>" /> 
                </span>
            </div>

            <?php FormHelper::TextField('onair_episode_no', $address->onair_episode_no, "On Air Episode No.", '', 'number', 1) ?>
<?php
	}
?>
	</fieldset>
    	<fieldset class="ui-widget-content">
    		<legend class="ui-widget-header">Address Information</legend>
			<?php FormHelper::TextField('venue_name_chinese', $address->venue_name_chinese, 'Venue Name (Chinese)', false, 'ruler input_status', 2) ?>
			<?php FormHelper::TextField('venue_name_english', $address->venue_name_english, 'Venue Name (English)', false, 'ruler input_status', 2) ?>
<?php
	if ($act != 'view')
	{
		$arr = PDES_GD::$venue_category;
	}
	else
	{
		PDES_GD::$venue_category[0] = '';
		$arr = PDES_GD::$venue_category;
	}
?>
        	<?php FormHelper::DropDownList('venue_category', $arr, $address->venue_category, 'Venue Category'); ?>
			<div class="form-item">
                <span class="grid-l"><label>Country</label> :</span>
                <span class="grid-r">
				    <input type="hidden" id="country" name="country" value="<?php echo $address->country;?>" />
				<?php if ($act === "view"){ ?>
                    <p><?php echo $address->country_name;?></p>
                <?php }else{ ?>
				    <input id="country_autocomplete" name="country_autocomplete" type="text" class="text" autocomplete="off" value="<?php echo $address->country_name;?>" />
                <?php } ?>
                </span>
            </div>

            <div class="form-item">
                <span class="grid-l"><label>Province/State/Prefecture</label> :</span>
                <span class="grid-r">
				    <input type="hidden" id="province_state" name="province_state" value="<?php echo $address->province_state_prefecture;?>" />
				<?php if ($act === "view"){ ?>
                    <p><?php echo $address->province_name;?></p>
                <?php }else{ ?>
                    <input id="province_state_autocomplete" name="province_state_autocomplete" type="text" class="text" autocomplete="off" value="<?php echo $address->province_name;?>" /> 
                <?php } ?>
                </span>
            </div>
			<?php 
				FormHelper::TextField('city', $address->city, 'City');
				FormHelper::TextField('street_line_1', $address->street_line_1, 'Street Line 1');
				FormHelper::TextField('street_line_2', $address->street_line_2, 'Street Line 2');
				FormHelper::TextField('phone_number', $address->phone_number, 'Phone Number');
				FormHelper::TextField('fax_number', $address->fax_number, 'Fax Number');
				FormHelper::TextField('url', $address->url, 'URL');
				FormHelper::TextField('email', $address->email, 'Email') ?>
			<?php # FormHelper::TextField('contact_person', '', 'Contact Person(s)') ?>
			<?php
                $arr_cast_type = array(
                    'contact_person' => 'Contact Person(s)'
                );
            ?>
    		<?php FormHelper::GlobalNameInput($arr_cast_type, $address_json_list, 1) ?>
            <?php 
				FormHelper::TextArea('useful_information', $address->useful_information, 'Useful Information');
				FormHelper::TextArea('description', $address->description, 'Description');
			?>
            <?php 
            if(in_array($data['_gn'],array('admin','proofreader','editor'))){
                if (!is_array(@$address->publish_datetime)) {
                    $address->publish_datetime = CommonUtils::formatDate(@$address->publish_datetime);
                }
				if (!is_array(@$address->expire_datetime)) {
                    $address->expire_datetime = CommonUtils::formatDate(@$address->expire_datetime);
				}
		    	FormHelper::DateTimePicker('publish_datetime', $address->publish_datetime, 'Publish Date/Time (YYYY-MM-DD / HH:MM)', @$address_diff['publish_datetime'], 1);
			    FormHelper::DateTimePicker('expire_datetime', $address->expire_datetime, 'Expiry Date/Time (YYYY-MM-DD / HH:MM)', @$address_diff['expire_datetime'], 1);
            }
            ?>
		</fieldset>
<?php
	if ($act != "add")
	{
?>
    	<fieldset class="ui-widget-content">
    		<legend class="ui-widget-header">Record Status and Access Log</legend>
        	    <?php FormHelper::ProtectedField(AC::$op_status[$address->status], 'Record Status', true) ?>
			    <?php FormHelper::ProtectedField($address->created_by, 'Created By') ?>
        	    <?php FormHelper::ProtectedField($address->created_at, 'Created At') ?>
         	    <?php FormHelper::ProtectedField($address->modified_by, 'Modified By') ?>       
        	    <?php FormHelper::ProtectedField($address->modified_at, 'Modified At') ?>

		</fieldset>
<?php
	}
?>

        <?php FormHelper::DisplayPublishLog($data['publishLog']); ?>
        <span class="buttons">
            <?php
                switch ($act) {
                    case 'add':
                        FormHelper::SubmitButton('create', 'Create'); 
                        FormHelper::ResetButton('reset', 'Reset');
                    break;
                    case 'edit':
                        FormHelper::SubmitButton('update', 'Update');
                        FormHelper::ResetButton('reset', 'Reset');
                    	FormHelper::Button('back', 'Back to list');
                    break;
                    case 'view':
						/** modified by vanni for bug no:260 **/
						if($publish ==1){
                            CommonUtils::showUnpublishButton($data, $data['_g']);
                        }else{
							if($address->status == AC::STATUS_DELETED){
								FormHelper::SubmitButton('recover', 'Recover');
							}else{
								FormHelper::SubmitButton('edit', 'Edit');
                                CommonUtils::showDeleteButton($data['publishLog']);
							}
						}
		            	FormHelper::Button('back', 'Back to list');
						break;
                }
            ?>
        </span>
    <?php FormHelper::EndForm() ?>

    </div>

</div>

<script>
    TVB.Pdes.oValidation = {
        form: document.forms['ane'],
        rules: <?php echo json_encode($validation_rules); ?>
    };
</script>
